package com.dajie.pt.controller.invitation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dajie.common.DictionaryServicesContext;
import com.dajie.common.dictionary.service.DictService;
import com.dajie.pt.controller.InvitationBaseController;
import com.dajie.pt.file.FileOperateUtil;
import com.dajie.pt.model.InvitationApply;
import com.dajie.pt.service.InvitationApplyService;

@Controller
@RequestMapping(value = "/file")
public class FileOperateController extends InvitationBaseController
{

	static DictService dictService = DictionaryServicesContext.getInstance().getDictService();

	@Autowired
	InvitationApplyService<InvitationApply, Integer> invitationApplyService;

	@RequestMapping(value = "to_upload")
	public ModelAndView toUpload()
	{
		return new ModelAndView("background/fileOperate/upload");
	}

	@RequestMapping(value = "upload")
	@ResponseBody
	public String upload(HttpServletRequest request, @RequestParam(value = "mobile", required = true) String mobile)
	{
		String filename = "";
		try
		{
			InvitationApply ia = invitationApplyService.findUniqueBy("mobile", mobile);
			filename= "local:" + FileOperateUtil.upload(request,ia);
			LOGGER.info("upload local file :{}",filename);
			ia.setResumeFile(filename);
			invitationApplyService.saveOrUpdate(ia);
		} catch (Exception e)
		{
			LOGGER.error("upload error:{}",e);
			return "error";
		}
		return "<script type=\"text/javascript\">parent.UPLOAD_CALLBACK('"+filename+"')</script>";
	}

	@RequestMapping(value = "download")
	public ModelAndView download(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "filename", required = true) String filename) throws Exception
	{
		ModelAndView mv = new ModelAndView("/file/download");
		String ctxPath = FileOperateUtil.getUploadDir(request);
		String storePath = ctxPath + filename + ".xml";
		String realName = filename + ".xml";
		String contentType = "application/octet-stream";
		LOGGER.error("storePath:{}", storePath);
		FileOperateUtil.download(request, response, storePath, contentType, realName);

		return mv;
	}

	@RequestMapping(value = "downloadall")
	public ModelAndView downloadall(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "filename", required = true) String filename) throws Exception
	{
		ModelAndView mv = new ModelAndView("/file/download");
		String storePath = filename;
		String realName = filename.substring(filename.lastIndexOf('/'));

		String contentType = "application/octet-stream";
		LOGGER.error("storePath all:{}", storePath);
		FileOperateUtil.zip(filename.substring(0, filename.lastIndexOf('/')) + "/resumeall", filename);
		FileOperateUtil.download(request, response, storePath, contentType, realName);

		return mv;
	}

	@RequestMapping(value = "downloadbyfilename")
	public ModelAndView downloadByFileName(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "filename", required = true) String filename) throws Exception
	{
		ModelAndView mv = new ModelAndView("/file/download");
		String storePath = filename ;
		String realName = filename.substring(filename.lastIndexOf('/'));

		String contentType = "application/octet-stream";
		LOGGER.info("storePath all:{}", storePath);
		FileOperateUtil.download(request, response, storePath, contentType, realName);
		return mv;
	}

}